package com.muyu.system.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.common.core.enums.OrderStatus;
import com.muyu.common.core.enums.PayMethod;
import com.muyu.system.domain.DanmoMarketOrder;
import com.muyu.system.domain.DanmoMarketOrderDetails;
import com.muyu.system.domain.DanmoMarketProducts;
import com.muyu.system.mapper.DanmoMarketOrderDetailsMapper;
import com.muyu.system.mapper.DanmoMarketOrderMapper;
import com.muyu.system.mapper.DanmoMarketProductsMapper;
import com.muyu.system.service.IDanmoMarketOrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 书画市场订单详情Service业务层处理
 *
 * @author kunzong
 * @date 2024-12-04
 */
@Service
public class DanmoMarketOrderDetailsServiceImpl implements IDanmoMarketOrderDetailsService {
    @Autowired
    private DanmoMarketOrderDetailsMapper danmoMarketOrderDetailsMapper;

    @Autowired
    private DanmoMarketOrderMapper danmoMarketOrderMapper;

    @Autowired
    private DanmoMarketProductsMapper danmoMarketProductsMapper;

    /**
     * 查询书画市场订单详情
     *
     * @param id 书画市场订单详情主键
     * @return 书画市场订单详情
     */
    @Override
    public DanmoMarketOrderDetails selectDanmoMarketOrderDetailsById(Long id) {
        return danmoMarketOrderDetailsMapper.selectDanmoMarketOrderDetailsById(id);
    }

    /**
     * 查询书画市场订单详情列表
     *
     * @param danmoMarketOrderDetails 书画市场订单详情
     * @return 书画市场订单详情
     */
    @Override
    public List<DanmoMarketOrderDetails> selectDanmoMarketOrderDetailsList(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        return danmoMarketOrderDetailsMapper.selectDanmoMarketOrderDetailsList(danmoMarketOrderDetails);
    }

    /**
     * 新增书画市场订单详情
     *
     * @param danmoMarketOrderDetails 书画市场订单详情
     * @return 结果
     */
    @Override
    public int insertDanmoMarketOrderDetails(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        danmoMarketOrderDetails.setCreateTime(DateUtils.getNowDate());
        return danmoMarketOrderDetailsMapper.insertDanmoMarketOrderDetails(danmoMarketOrderDetails);
    }

    /**
     * 修改书画市场订单详情
     *
     * @param danmoMarketOrderDetails 书画市场订单详情
     * @return 结果
     */
    @Override
    public int updateDanmoMarketOrderDetails(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        danmoMarketOrderDetails.setUpdateTime(DateUtils.getNowDate());
        return danmoMarketOrderDetailsMapper.updateDanmoMarketOrderDetails(danmoMarketOrderDetails);
    }

    /**
     * 批量删除书画市场订单详情
     *
     * @param ids 需要删除的书画市场订单详情主键
     * @return 结果
     */
    @Override
    public int deleteDanmoMarketOrderDetailsByIds(Long[] ids) {
        return danmoMarketOrderDetailsMapper.deleteDanmoMarketOrderDetailsByIds(ids);
    }

    /**
     * 删除书画市场订单详情信息
     *
     * @param id 书画市场订单详情主键
     * @return 结果
     */
    @Override
    public int deleteDanmoMarketOrderDetailsById(Long id) {
        return danmoMarketOrderDetailsMapper.deleteDanmoMarketOrderDetailsById(id);
    }

    /**
     * 新增物流单号
     *
     * @param danmoMarketOrderDetails
     * @return
     */
    @Override
    public Result updateDanmoShoppingLogisticsOrderNumber(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        Long orderId = danmoMarketOrderDetails.getOrderId();
        DanmoMarketOrderDetails danmoMarketOrderDetailsThis = danmoMarketOrderDetailsMapper.selectDanmoMarketOrderDetailsByOrderId(orderId);

        if (danmoMarketOrderDetailsThis == null) {
            return Result.error("订单详情未找到");
        }

        DanmoMarketOrder danmoMarketOrder = danmoMarketOrderMapper.selectSingleOrder(orderId);
        if (danmoMarketOrder == null) {
            return Result.error("市场订单未找到");
        }

        if (!danmoMarketOrderDetailsThis.equals(4L)) {
            if (danmoMarketOrder.getOrderStatus() != OrderStatus.TO_BE_SHIPPED)
                return Result.error("订单状态异常，请联系平台");
        }

        // 更新支付信息
        updateMarketOrderDelivery(danmoMarketOrder, danmoMarketOrderDetails);
        updateOrderDetailsdelivery(danmoMarketOrderDetailsThis, danmoMarketOrderDetails);

        // 插入订单并返回结果
        try {
            // 保存更新
            int i = danmoMarketOrderDetailsMapper.updateDanmoMarketOrderDetails(danmoMarketOrderDetailsThis);
            int insertResult = danmoMarketOrderMapper.updateDanmoMarketOrder(danmoMarketOrder);
            if (insertResult > 0 && i > 0) {
                LoginUser loginUser = SecurityUtils.getLoginUser();
                DanmoMarketProducts danmoMarketProducts = new DanmoMarketProducts();
                danmoMarketProducts.setId(danmoMarketOrderDetails.getProductId());
                danmoMarketProducts.setMarketState(4L);
                danmoMarketProducts.setUpdateBy(String.valueOf(loginUser.getUserid()));
                danmoMarketProducts.setUpdateTime(DateUtils.getNowDate());
                danmoMarketProductsMapper.updateDanmoMarketProducts(danmoMarketProducts);
                return Result.success(1, "发货成功");
            } else {
                return Result.error("发货失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发货失败: " + e.getMessage());
        }
    }

    /**
     * 订单详情对象(发货)
     *
     * @param danmoMarketOrderDetailsThis
     */
    private void updateOrderDetailsdelivery(DanmoMarketOrderDetails danmoMarketOrderDetailsThis, DanmoMarketOrderDetails danmoMarketOrderDetails) {
        danmoMarketOrderDetailsThis.setMarketState("已发货");
        danmoMarketOrderDetailsThis.setDeliveryTime(DateUtils.getNowDate());
        danmoMarketOrderDetailsThis.setPosExpress(danmoMarketOrderDetails.getPosExpress());
        danmoMarketOrderDetailsThis.setExpressDeliveryNumber(danmoMarketOrderDetails.getExpressDeliveryNumber());
    }

    /**
     * 用户订单对象(发货)
     *
     * @param danmoMarketOrder
     */
    private void updateMarketOrderDelivery(DanmoMarketOrder danmoMarketOrder, DanmoMarketOrderDetails danmoMarketOrderDetails) {
        danmoMarketOrder.setOrderStatus(OrderStatus.TO_BE_RECEIVED);
        danmoMarketOrder.setDeliveryTime(DateUtils.getNowDate());
        danmoMarketOrder.setPosExpress(danmoMarketOrderDetails.getPosExpress());
        danmoMarketOrder.setExpressDeliveryNumber(danmoMarketOrderDetails.getExpressDeliveryNumber());
    }

    /**
     * 付款
     *
     * @param danmoMarketOrderDetails
     * @return
     */
    @Override
    public Result updateDanmoShoppingOrderDetails(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        Long orderId = danmoMarketOrderDetails.getOrderId();

        DanmoMarketOrderDetails danmoMarketOrderDetailsThis = danmoMarketOrderDetailsMapper.selectDanmoMarketOrderDetailsByOrderId(orderId);
        if (danmoMarketOrderDetailsThis == null) {
            return Result.error("订单详情未找到");
        }

        DanmoMarketOrder danmoMarketOrder = danmoMarketOrderMapper.selectSingleOrder(orderId);
        if (danmoMarketOrder == null) {
            return Result.error("市场订单未找到");
        }

//        if (!danmoMarketOrderDetailsThis.getMarketState().equals("待付款") || danmoMarketOrder.getOrderStatus() != 1) {
//            return Result.error("订单状态异常，请重试");
//        }

        // 更新支付信息
        updateOrderDetails(danmoMarketOrderDetailsThis, danmoMarketOrderDetails.getPaymentMethod());
        updateMarketOrder(danmoMarketOrder, danmoMarketOrderDetails.getPaymentMethod());


        // 插入订单并返回结果
        try {
            // 保存更新
            int i = danmoMarketOrderDetailsMapper.updateDanmoMarketOrderDetails(danmoMarketOrderDetailsThis);
            int insertResult = danmoMarketOrderMapper.updateDanmoMarketOrder(danmoMarketOrder);
            if (insertResult > 0 && i > 0) {
                LoginUser loginUser = SecurityUtils.getLoginUser();
                DanmoMarketProducts danmoMarketProducts = new DanmoMarketProducts();
                danmoMarketProducts.setId(danmoMarketOrderDetails.getProductId());
//                danmoMarketProducts.setMarketState("待发货");
                danmoMarketProducts.setUpdateBy(String.valueOf(loginUser.getUserid()));
                danmoMarketProducts.setUpdateTime(DateUtils.getNowDate());
                danmoMarketProductsMapper.updateDanmoMarketProducts(danmoMarketProducts);
                return Result.success(1, "支付成功");
            } else {
                return Result.error("支付失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("支付失败: " + e.getMessage());
        }
    }



    /**
     * 收货
     *
     * @param danmoMarketOrderDetails
     * @return
     */
    @Override
    public Result updateDanmoShoppingReceiving(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        Long orderId = danmoMarketOrderDetails.getOrderId();
        DanmoMarketOrderDetails danmoMarketOrderDetailsThis = danmoMarketOrderDetailsMapper.selectDanmoMarketOrderDetailsByOrderId(orderId);

        if (danmoMarketOrderDetailsThis == null) {
            return Result.error("订单详情未找到");
        }

        DanmoMarketOrder danmoMarketOrder = danmoMarketOrderMapper.selectSingleOrder(orderId);
        if (danmoMarketOrder == null) {
            return Result.error("市场订单未找到");
        }

        if (danmoMarketOrderDetailsThis.equals("已发货") || danmoMarketOrder.getOrderStatus() != OrderStatus.TO_BE_RECEIVED) {
            return Result.error("订单状态异常，请联系平台");
        }

        // 更新支付信息
        updateMarketOrderReceiving(danmoMarketOrder);
        updateOrderDetailsReceiving(danmoMarketOrderDetailsThis);

        // 插入订单并返回结果
        try {
            // 保存更新
            int i = danmoMarketOrderDetailsMapper.updateDanmoMarketOrderDetails(danmoMarketOrderDetailsThis);
            int insertResult = danmoMarketOrderMapper.updateDanmoMarketOrder(danmoMarketOrder);
            if (insertResult > 0 && i > 0) {
                LoginUser loginUser = SecurityUtils.getLoginUser();
                DanmoMarketProducts danmoMarketProducts = new DanmoMarketProducts();
                danmoMarketProducts.setId(danmoMarketOrderDetails.getProductId());
//                danmoMarketProducts.setMarketState("已完成");
                danmoMarketProducts.setUpdateBy(String.valueOf(loginUser.getUserid()));
                danmoMarketProducts.setUpdateTime(DateUtils.getNowDate());
                danmoMarketProductsMapper.updateDanmoMarketProducts(danmoMarketProducts);
                return Result.success(1, "收货成功");
            } else {
                return Result.error("收货失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("收货失败: " + e.getMessage());
        }
    }

    /**
     * 退货/售后
     *
     * @param danmoMarketOrderDetails
     * @return
     */
    @Override
    public Result updateDanmoShoppingAfterSales(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        return null;
    }

    /**
     * 订单详情对象(收获)
     *
     * @param danmoShoppingOrderDetails
     */
    private void updateOrderDetailsReceiving(DanmoMarketOrderDetails danmoShoppingOrderDetails) {
        danmoShoppingOrderDetails.setMarketState("已完成");
        danmoShoppingOrderDetails.setTransactionTime(DateUtils.getNowDate());
    }

    /**
     * 用户订单对象(收货)
     *
     * @param danmoMarketOrder
     */
    private void updateMarketOrderReceiving(DanmoMarketOrder danmoMarketOrder) {
        danmoMarketOrder.setOrderStatus(OrderStatus.TO_BE_EVALUATED);
        danmoMarketOrder.setTransactionTime(DateUtils.getNowDate());
    }

    private void updateOrderDetails(DanmoMarketOrderDetails orderDetails, Long paymentMethod) {
        orderDetails.setPaymentTime(DateUtils.getNowDate());
        orderDetails.setPaymentMethod(paymentMethod);
        orderDetails.setMarketState("待发货"); // 使用常量
    }

    private void updateMarketOrder(DanmoMarketOrder marketOrder, Long paymentMethod) {
        marketOrder.setPaymentTime(DateUtils.getNowDate());
        marketOrder.setPaymentMethod(PayMethod.getPayChannel(paymentMethod.intValue()));
        marketOrder.setOrderStatus(OrderStatus.TO_BE_SHIPPED); // 使用常量
    }
}
